package com.group.patients.controller;

import com.group.commons.entity.patient.Family;
import com.group.commons.entity.patient.Patient;
import com.group.commons.util.ResponseInfo;
import com.group.patients.service.MainService;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * @author: user  <br/>
 * Date: 2022/6/7:15:54  <br/>
 * Description:
 */
@RestController
@RequestMapping("/patient")
public class MainController {
    @Resource
    MainService mainService;

    @RequestMapping("/getData")
    public ResponseInfo<PageImpl<Patient>> getData(@RequestBody Map<String, String> map) {
        System.out.println(map);
        System.out.println("--------------------------------------------------------------------");
        System.out.println(getPageRequest(map));
        System.out.println("--------------------------------------------------------------------");
        System.out.println(getPatient(map));
        System.out.println("--------------------------------------------------------------------");
        return mainService.getData(getPatient(map), getPageRequest(map));
    }

    //因为requestBody只能有一个，故写方法手动映射
    PageRequest getPageRequest(Map<String, String> map) {
        PageRequest of = PageRequest.of(Integer.parseInt(map.get("page")), Integer.parseInt(map.get("size")));
        return of;
    }

    Patient getPatient(Map<String, String> map) {
        Family family = new Family();
        family.setName(map.get("name"));
        family.setAddr(map.get("addr"));
        if (map.get("idCard") != null) {
            family.setidCard(Integer.parseInt(map.get("idCard")));
        }
        if (map.get("socialsecuritycard") != null) {
            family.setsocialsecuritycard(Integer.parseInt(map.get("socialsecuritycard")));
        }
        if (map.get("tel") != null) {
            family.setTel(Integer.parseInt(map.get("tel")));
        }
        return new Patient(map.get("account"), Arrays.asList(family));
    }
}

